<template>
  <div>
    <div class="web_bg">
      <div class="main-grid animated fadeInUp">

        <div style="margin-top: 4rem; padding-left: 1rem;">
          <div v-for="(card,index) in voiceList" :key="card.id">
            <voiceCard class="animated fadeInUp" :userName="card.username" :avartarUrl="card.avatar" :time="card.createTime"
              :content="card.voiceContent" :talkImage="card.voiceImage" :audioUrl="card.voiceUrl" />

          </div>

          <div class="pagination-wrap">
            <div @click="pageVoiceNext" class="pagination" v-if="pagination.total !== voiceList.length">
              下一页
            </div>
            <div v-else style="user-select: none">~~到底啦~~</div>
          </div>
        </div>
        <div class="col-span-1" style="margin-top: 60px;">
          <div>
            <div class="h-98 w-full rounded-2xl relative shadow-xl mb-8"
              style="background: linear-gradient(130deg, rgb(36, 198, 220), rgb(84, 51, 255) 41.07%, rgb(255, 0, 153) 76.05%);">
              <div
                class="ob-gradient-cut-plate absolute bg-ob-deep-900 rounded-xl opacity-90 flex justify-center items-center pt-4 px-6 shadow-lg hover:shadow-2xl duration-300"
                data-dia="author">
                <div class="profile absolute w-full flex flex-col justify-center items-center">
                  <div class="flex flex-col justify-center items-center"><img class="ob-avatar circle-avatar"
                      :src="userStore.currentUser.userpic || userStore.currentUser.avatar">
                    <h2 class="text-center pt-4 text-4xl font-semibold text-ob-bright">林智卫 ლ(´ڡ`ლ)ﾞ
                    </h2>
                    <span class="h-1 w-14 rounded-full mt-2"
                      style="background: linear-gradient(130deg, rgb(36, 198, 220), rgb(84, 51, 255) 41.07%, rgb(255, 0, 153) 76.05%);"></span>
                    <p class="pt-6 px-10 w-full text-s text-center">一个低调的原神玩家。原神启动！
                    </p>
                  </div>
                  <div class="h-full w-full flex flex-col flex-1 justify-end items-end">
                    <ul class="flex flex-row justify-evenly flex-wrap w-full py-4 px-2 text-center items-center"
                      data-v-3bd8505e=""><a href="https://github.com/TXM983" target="_blank" data-v-3bd8505e="">
                        <li class="diamond-clip-path diamond-icon" data-v-3bd8505e=""><svg
                            class="svg-icon fill-current fill-current" aria-hidden="true" data-v-f2861448=""
                            data-v-3bd8505e="">
                            <use href="#icon-github" data-v-f2861448=""></use>
                          </svg></li>
                      </a><a href="https://gitee.com/segfuhjsgbfuijksgbjk" target="_blank" data-v-3bd8505e="">
                        <li class="diamond-clip-path diamond-icon" data-v-3bd8505e=""><svg
                            class="svg-icon fill-current fill-current" aria-hidden="true" data-v-f2861448=""
                            data-v-3bd8505e="">
                            <use href="#icon-gitee" data-v-f2861448=""></use>
                          </svg></li>
                      </a><!----><!----><!----><!----><!----><!----><!----><!----></ul>
                    <ul class="grid grid-cols-4 pt-4 w-full px-2 text-lg">
                      <li class="col-span-1 text-center"><span class="text-ob-bright">4</span>
                        <p class="text-base">文章</p>
                      </li>
                      <li class="col-span-1 text-center"><span class="text-ob-bright">2</span>
                        <p class="text-base">说说</p>
                      </li>
                      <li class="col-span-1 text-center"><span class="text-ob-bright">4</span>
                        <p class="text-base">分类</p>
                      </li>
                      <li class="col-span-1 text-center"><span class="text-ob-bright">6</span>
                        <p class="text-base">标签</p>
                      </li>
                    </ul>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <Footer></Footer>
  </div>
</template>
<script setup lang='ts'>

  import Footer from '@/pages/navbar/common/Footer.vue'
  import voiceCard from '@/pages/navbar/voice/components/voiceCard.vue'
  import { ref, onMounted, onUnmounted } from 'vue'
  import { reqListVoice } from '@/api/voice'
  import useUserStore from '@/store/modules/user'
  let userStore = useUserStore()

  const voiceList = ref([])

  const pagination = ref({
    current: 1,
    size: 3,
    total: 0,
  });

  onMounted(() => {
    getVoiceList();
    window.addEventListener('scroll', checkScroll);
  })

  onUnmounted(() => {
    window.removeEventListener('scroll', checkScroll);
  })

  const pageVoiceNext = () => {
    pagination.value.current += 1
    getVoiceList()
  }

  const getVoiceList = async () => {
    let result = await reqListVoice(pagination.value);
    if (result.code === 200) {
      voiceList.value = voiceList.value.concat(result.data.records);
      pagination.value.total = result.data.total;
    }
  }

  // 返回页面最底部的辅助函数
  const isBottomOfPage = () => {
    return window.innerHeight + window.scrollY >= document.documentElement.offsetHeight;
  }

  // 监测滚动并确定是否到达底部
  const checkScroll = () => {
    // 若页面滚动到底部，且还有更多数据可以加载
    if (isBottomOfPage() && (voiceList.value.length < pagination.value.total)) {
      // 分页加一
      pagination.value.current++;
      // 获取下一页的数据
      getVoiceList();
    }
  }

</script>
<style scoped>
  @import url(./index.css);

  .relative {
    position: relative
  }

  .inverted-main-grid,
  .main-grid {
    display: flex;
    flex-direction: column
  }

  @media(min-width: 1024px) {
    .main-grid {
      display: grid;
      gap: 2rem;
      grid-template-columns: minmax(0, 1fr) 320px
    }

    .inverted-main-grid {
      display: grid;
      gap: 2rem;
      grid-template-columns: 245px minmax(0, 1fr)
    }
  }

  .web_bg {
    z-index: -999;
    width: 100%;
    height: 100%;
    background: url(https://s2.loli.net/2023/10/02/UgKNjLrGic13daS.webp);
    background-position-x: 0%;
    background-position-y: 0%;
    background-repeat: repeat;
    background-attachment: scroll;
    background-size: auto;
    background-attachment: local;
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
  }

  .pagination-wrap {
    display: flex;
    justify-content: center;
    margin-top: 40px;
  }

  .pagination {
    padding: 13px 15px;
    border: 1px solid var(--lightGray);
    border-radius: 3rem;
    color: var(--greyFont);
    width: 100px;
    user-select: none;
    cursor: pointer;
    text-align: center;
  }

  .pagination:hover {
    border: 1px solid var(--themeBackground);
    color: var(--themeBackground);
    box-shadow: 0 0 5px var(--themeBackground);
  }
</style>